package com.traffic.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Status;
import com.traffic.entity.Label;
import com.traffic.entity.Road;

import com.traffic.exception.BaseException;
import com.traffic.model.ApiResponse;

import com.traffic.service.LabelService;
import com.traffic.service.RoadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;



@Api(tags = "路况相关接口")
@RestController
@RequestMapping(value = "/road")
@Slf4j

public class RoadController {

    @Autowired
    RoadService roadService;

    @Autowired
    LabelService labelService;

    @ApiOperation(value = "【路况】", notes = "上报路况")
    @PostMapping("/insert")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =Integer.class,key = "label_id",example = "3" , description = "路况属于的标签id"),
            @ApiJsonProperty(type =String.class,key = "address",example = "杭州市xx区xx街道xx路xx" , description = "地址"),
            @ApiJsonProperty(type =String.class,key = "content",example = "堵得很厉害" , description = "描述"),
            @ApiJsonProperty(type =Double.class,key = "longitude",example = "118.23456" , description = "经度"),
            @ApiJsonProperty(type =Double.class,key = "latitude",example = "30.34567" , description = "纬度")

    })
    public ApiResponse insertRoad(@RequestBody Map<String, Object> road) {

        String param[] = new String[]{"label_id","address","content","longitude","latitude"};

        List<String> paramList = CollUtil.newArrayList(param);
        if (!paramList.containsAll(CollUtil.newArrayList(road.keySet().toArray()))) {
            throw new BaseException(Status.PARAM_ERROR);
        }

        Road ro = new Road();
        BeanUtil.copyProperties(road, ro, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        ro.setUp(0);
        ro.setDown(0);
        ro.setState(0);
        ro.setCreateTime(new DateTime());
        roadService.save(ro);

        Label lab =  labelService.getById(ro.getLabelId());
        //lab.setAmount(lab.getAmount()+1);
        labelService.updateById(lab);

        return ApiResponse.ofStatus(Status.OK);
    }


    @ApiOperation(value = "【路况】", notes = "点赞")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "upId", value = "点赞的路况id", required = true)
    @GetMapping("/up/{upId}")
    public ApiResponse upRoad(@PathVariable Long upId) {

        Road ro = roadService.getById(upId);
        ro.setUp(ro.getUp()+1);
        roadService.updateById(ro);

        return ApiResponse.ofStatus(Status.OK);
    }

    @ApiOperation(value = "【路况】", notes = "吐槽")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "downId", value = "吐槽的路况id", required = true)
    @GetMapping("/down/{downId}")
    public ApiResponse downRoad(@PathVariable Long downId) {

        Road ro = roadService.getById(downId);
        ro.setDown(ro.getDown()+1);
        roadService.updateById(ro);
        return ApiResponse.ofStatus(Status.OK);
    }

    @ApiOperation(value = "【路况】", notes = "获得附近路况信息")
    @PostMapping("/getRoad")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =Double.class,key = "longitude",example = "110.36586" , description = "经度"),
            @ApiJsonProperty(type =Double.class,key = "latitude",example = "30.36586" , description = "纬度")
    })
    public ApiResponse getRoad(@RequestBody Map<String, String> param) {

        //1°大约111km,0.01=11.1km
        Float longitude = new Float(param.get("longitude"));
        Float latitude = new Float(param.get("latitude"));

        List<Road> roads= roadService.list(
                new QueryWrapper<Road>().between("longitude",longitude-0.01,longitude+0.01)
                        .between("latitude",latitude-0.01,latitude+0.01).eq("state",0)
        );



        if (roads.isEmpty()) {
            return ApiResponse.ofStatus(Status.OK);
        }else {
            return ApiResponse.ofStatus(Status.OK, roads);
        }

    }

    



}
